import { _decorator, Component, instantiate, Label, Layout, Node, ScrollView, Slider, Sprite, Toggle, Vec2, Vec3 } from 'cc';
import { UIManager } from '../UIManager';
import { LanauageManager } from '../LanauageManager';
import { GameData, taskCfg } from '../GameData';
import { HttpClient } from '../net/HttpClient';
import { EventManager } from '../EventManager';
import { AudioMgr } from '../AudioMgr';
import { taskItem } from '../item/taskItem';
const { ccclass, property } = _decorator;

@ccclass('popupTask')
export class popupTask extends Component {

    @property(ScrollView)
    scrollView:ScrollView = null;

    taskList:taskCfg[];

    selctType:number = 0

    start() {
        EventManager.eventTarget.on(EventManager.REFRESH_TASKCFGS, this.updateTask, this);
    }

    onEnable() {
        this.updateTask();
    }

    updateTask(){
        HttpClient.getInstance().sendGetTaskList(this.selctType,(data)=>{
            this.taskList = data.data;
            this.refresh();
        })
    }

    refresh(){
        for (let index = 0; index < this.scrollView.content.children.length; index++) {
            const element = this.scrollView.content.children[index];
            element.active = false;
        }

        for (let index = 0; index < this.taskList.length; index++) {
            let taskData:taskCfg = this.taskList[index];
            let item = this.scrollView.content.children[index];
            if (!item){
                item = instantiate(this.scrollView.content.children[0])
                item.parent = this.scrollView.content;
            }
            item.active = true;
            item.getComponent(taskItem).refresh(taskData);
        }
    }

    onClickHandler(event:Event, str:string){
        if (str == "getReward"){

        }
        else if (str == "tog1"){
            this.selctType = 0;
        }
        else if (str == "tog2"){
            this.selctType = 1;
        }
        else if (str == "tog3"){
            this.selctType = 2;
        }
        this.updateTask();
    }
}

